package com.tool_stdy.RedisTest;

import com.tool_stdy.Model.User;
import com.tool_stdy.utils.JedisUtils;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yan_li on 2016/3/22.
 */
public class UserDao {

    static Jedis jedis = JedisUtils.getPool().getResource();

    public static void saveUser() {
        User u = new User();
        u.setId(2);
        u.setName("lisi");
        u.setAge(10);
        u.setAdress("山东省");
        u.setSex(1);
        Map<String, String> parames = new HashMap<String, String>();
        parames.put("name", u.getName());
        parames.put("id", u.getId() + "");
        parames.put("age", u.getAge() + "aaa");
        parames.put("adress", u.getAdress());
        parames.put("sex", u.getSex() + "");
        jedis.hmset("userInfo:" + u.getId(), parames);
    }

    public static User getUser(int id) {
        User u = new User();
        Map<String, String> stringStringMap = jedis.hgetAll("userInfo:" + id);
        System.out.println(stringStringMap);
        return u;
    }

    public static void updateUserInfo(int id, String field, String value) {
        Long hset = jedis.hset("userInfo:" + id, field, value);
        System.out.println("dd" + hset);
    }

    public static void delUser(String id) {
        jedis.hdel(id, "name", "age", "adress", "sex", "id");
    }

    public static void setExpireTime(long time, int id) {
        jedis.expire("userInfo:" + id, Integer.valueOf(String.valueOf(time)));
    }

    public static void main(String[] args) {
        saveUser();
        // System.out.println("存储成功,获取:");
        // getUser(2);
        // System.out.println("更新地址");
        // setExpireTime(100,2);
        // updateUserInfo(1, "adress", "北京市");
        // System.out.println("更新后");
        getUser(2);

    }
}
